from flask import Flask, request, render_template, redirect
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import pymysql

import random

host = 'http://0.0.0.0:7090/'
app = Flask (__name__)



@app.route('/hello')
def hello() -> str:
    return 'https://www.tingfengcloud.com'

@app.route('/')
def index():
    return render_template('login.html')


@app.route('/login',methods=['POST'])
def login():
    if request.method == 'POST':
        do_login()
    else:
        return 'gun'

def do_login():
    mobile = request.form.get('mobile')
    print(mobile)


    #return render_template('index1.html')

    client = AcsClient('LTAI4GHmyxuKc3VUXjz3mGGo', 'GyCsq6ueJG4IAkjHeld9IIMNb19pop', 'cn-hangzhou')
    commonrequest = CommonRequest()
    commonrequest.set_accept_format('json')
    commonrequest.set_domain('dysmsapi.aliyuncs.com')
    commonrequest.set_method('POST')
    commonrequest.set_protocol_type('https')  # https | http
    commonrequest.set_version('2017-05-25')
    commonrequest.set_action_name('SendSms')

    # 自定义参数
    commonrequest.add_query_param("PhoneNumbers", mobile)
    commonrequest.add_query_param("SignName", '听风Cloud')
    commonrequest.add_query_param("TemplateCode", 'SMS_190788243')

    code = ""
    for num in range(1, 5):
        code = code + str(random.randint(0, 9))
    send_code = '{"code":"' + code + '"}'
    commonrequest.add_query_param('TemplateParam', send_code)

    response = client.do_action_with_exception(commonrequest)

    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    return response


#
app.run(port=7090 ,host='0.0.0.0')


